package cn.huaqingcheng.tianshu.application.http;

import cn.huaqingcheng.tianshu.core.start.model.Site;
import cn.huaqingcheng.tianshu.core.start.param.SiteParam;
import cn.huaqingcheng.tianshu.core.start.param.SortParam;
import cn.huaqingcheng.tianshu.core.start.service.SiteService;
import cn.huaqingcheng.tianshu.security.annotation.CurrentAccountId;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 站点图标
 */
@RestController
@RequiredArgsConstructor
public class SiteController {

    private final SiteService siteService;


    /**
     * 查询指定分组下的所有
     */
    @GetMapping("/site/{groupId:\\d+}")
    public List<Site> listSite(@PathVariable("groupId") Long groupId, @CurrentAccountId Long accountId) {
        return siteService.getList(accountId, groupId);
    }

    /**
     * 新增站点
     */
    @PostMapping("/site")
    public Long createSite(@RequestBody @Valid SiteParam site, @CurrentAccountId Long accountId) {
        return siteService.create(site, accountId).getId();
    }

    /**
     * 更新站点
     */
    @PutMapping("/site")
    public Long updateSite(@RequestBody @Valid SiteParam site, @CurrentAccountId Long accountId) {
        return siteService.update(site, accountId).getId();
    }

    /**
     * 更新站点排序
     */
    @PutMapping("/site/sort")
    public void updateSiteSort(@RequestBody List<SortParam> params, @CurrentAccountId Long accountId) {
        siteService.updateSort(params, accountId);
    }

    /**
     * 删除站点
     */
    @DeleteMapping("/site/{id:\\d+}")
    public void removeSite(@PathVariable("id") Long id, @CurrentAccountId Long accountId) {
        siteService.remove(List.of(id), accountId);
    }

    /**
     * 删除站点
     */
    @DeleteMapping("/site")
    public void removeSites(@RequestParam("id") List<Long> ids, @CurrentAccountId Long accountId) {
        siteService.remove(ids, accountId);
    }

}
